//
//  CarListView.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/4/7.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import UIKit
//审车 审证 补证 车险代办等等
class CarListView: UIView {
    
    var carArray = Array<CarModel>()
    var startOffsetX : CGFloat = 0

    override init(frame: CGRect){
        
        super.init(frame: frame)
        
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = .init(width: kScreenW - 30, height: 90)
        layout.sectionInset = .init(top: 5, left: 5, bottom: 5, right: 5)
        layout.scrollDirection = .horizontal
        
        let collectionView = UICollectionView.init(frame: .init(x: 0, y: 0, width: kScreenW - 20 , height: 100), collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor(red: 243/255, green: 243/255, blue: 243/255, alpha: 1.0)
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.tag = 2
        collectionView.register(CarViewCell.self, forCellWithReuseIdentifier: "car")
        self.addSubview(collectionView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置图片
    func setCarList(carArray: Array<CarModel>) {
        self.carArray = carArray
        let collect:UICollectionView = self.viewWithTag(2) as! UICollectionView
        collect.reloadData()
    }
}
extension CarListView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.carArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let hvc = HomeViewController()
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "car", for: indexPath) as! CarViewCell
        if self.carArray.count > 0 {
            let index = indexPath.row
            let car: CarModel = self.carArray[index]
            cell.img.sd_setImage(with: URL.init(string: car.pic_url), placeholderImage: UIImage.init(named: "car_icon"))
            cell.carnum.text = car.carnum
            cell.remark.text = car.remark
            cell.edit.tag = index
            cell.edit.addTarget(hvc, action: #selector(hvc.editCarClick(sender:)), for: UIControlEvents.touchUpInside)
        }
        return cell
    }
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        var targetIndex : Int = 0
        var startIndex : Int = 0
        let currentOffSetX = scrollView.contentOffset.x
        let scrollViewW = scrollView.bounds.width
        let offsetX = currentOffSetX - startOffsetX
        if offsetX > 0 { //向左滑动
            // 1.计算startIndex
            startIndex = Int(currentOffSetX / scrollViewW)

            // 2.计算targetIndex
            targetIndex = startIndex + 1
            if targetIndex >= carArray.count {
                targetIndex = carArray.count - 1
            }
        }
        else{ //向右滑动
            // 1.计算targetIndex
            targetIndex = Int(currentOffSetX / scrollViewW)

            // 2.计算startIndex
            startIndex = targetIndex + 1
            if startIndex >= carArray.count {
                startIndex = carArray.count - 1
            }
        }
        startOffsetX = CGFloat(targetIndex) * (kScreenW - 20)
        targetContentOffset.pointee.x = startOffsetX
        targetContentOffset.pointee.y = 0
    }
}
